using System;
using System.Drawing;
using Spread.Common.Features.Sample;
using FarPoint.Win.Spread;
using FarPoint.Win;
using FarPoint.Win.Spread.Model;
using FarPoint.Win.Chart;

namespace Spread.Printing.Features
{
    public partial class IntroPrint : BaseSampleControl
    {
        #region ctor
        public IntroPrint()
        {
            InitializeComponent();
            Name = "Introduction";

            InitSpreadSheet(fpSpread.ActiveSheet);

            btnPrint.Click += BtnPrint_Click;
        }
        #endregion

        #region Private methods
        private void InitSpreadSheet(SheetView sheet)
        {
            fpSpread.SuspendLayout();

            fpSpread.AllowCellOverflow = true;
            sheet.SetMultipleRowHeights(0, sheet.RowCount, 24);

            sheet.AddSpanCell(1, 1, 1, 3);
            sheet.Cells[1, 1].Text = "Store";
            sheet.AddSpanCell(1, 4, 1, 7);
            sheet.Cells[1, 4].Text = "Goods";
            sheet.AddSpanCell(2, 1, 1, 2);
            sheet.Cells[2, 1].Text = "Area";
            sheet.AddSpanCell(2, 3, 2, 1);
            sheet.Cells[2, 3].Text = "ID";
            sheet.AddSpanCell(2, 4, 1, 2);
            sheet.Cells[2, 4].Text = "Fruits";
            sheet.AddSpanCell(2, 6, 1, 2);
            sheet.Cells[2, 6].Text = "Vegetables";
            sheet.AddSpanCell(2, 8, 1, 2);
            sheet.Cells[2, 8].Text = "Foods";
            sheet.AddSpanCell(2, 10, 2, 1);
            sheet.Cells[2, 10].Text = "Total";

            sheet.Cells[3, 1].Text = "State";
            sheet.Cells[3, 2].Text = "City";
            sheet.Cells[3, 4].Text = "Grape";
            sheet.Cells[3, 5].Text = "Apple";
            sheet.Cells[3, 6].Text = "Potato";
            sheet.Cells[3, 7].Text = "Tomato";
            sheet.Cells[3, 8].Text = "Sandwich";
            sheet.Cells[3, 9].Text = "Hamburger";

            sheet.AddSpanCell(4, 1, 7, 1);
            sheet.AddSpanCell(4, 2, 3, 1);
            sheet.AddSpanCell(7, 2, 3, 1);
            sheet.AddSpanCell(10, 2, 1, 2);
            sheet.Cells[10, 2].Text = "Sub Total:";
            sheet.AddSpanCell(11, 1, 7, 1);
            sheet.AddSpanCell(11, 2, 3, 1);
            sheet.AddSpanCell(14, 2, 3, 1);
            sheet.AddSpanCell(17, 2, 1, 2);
            sheet.Cells[17, 2].Text = "Sub Total:";
            sheet.AddSpanCell(18, 1, 1, 3);
            sheet.Cells[18, 1].Text = "Total:";

            sheet.Cells[4, 1].Text = "NC";
            sheet.Cells[4, 2].Text = "Raleigh";
            sheet.Cells[7, 2].Text = "Charlotte";
            for (int row = 4; row < 10; ++row)
            {
                sheet.Cells[row, 3].Text = $"{(row - 3):d3}";
            }
            sheet.Cells[11, 1].Text = "PA";
            sheet.Cells[11, 2].Text = "Philadelphia";
            sheet.Cells[14, 2].Text = "Pittsburgh";
            for (int row = 11; row < 17; ++row)
            {
                sheet.Cells[row, 3].Text = $"{(row - 4):d3}";
            }

            for (int col = 4; col < 10; ++col)
            {
                char colChar = (char)('A' + col);
                sheet.Cells[10, col].Formula = $"SUM({colChar}5:{colChar}10)";
                sheet.Cells[17, col].Formula = $"SUM({colChar}12:{colChar}17)";
            }

            for (int i = 0; i < 14; ++i)
            {
                sheet.Cells[4 + i, 10].Formula = $"SUM(E{5 + i}:J{5 + i})";
            }

            for (int col = 4; col < 11; ++col)
            {
                char colChar = (char)('A' + col);
                sheet.Cells[18, col].Formula = $"{colChar}11 + {colChar}18";
            }

            sheet.Cells[1, 1, 3, 10].BackColor = Color.FromArgb(217, 217, 255);
            sheet.Cells[4, 1, 18, 3].BackColor = Color.FromArgb(217, 255, 217);
            sheet.Cells[1, 1, 3, 10].HorizontalAlignment = CellHorizontalAlignment.Center;

            ComplexBorderFactory complexBorderFactory = new ComplexBorderFactory();
            var thinLineBorder = complexBorderFactory.CreateComplexBorder(new ComplexBorderSide(
                Color.Black, 1));
            var dottedLineBorder = complexBorderFactory.CreateComplexBorder(new ComplexBorderSide(
                ComplexBorderSideStyle.Dotted, Color.Black));

            CompoundBorder compoundBorder = new CompoundBorder(new LineBorder(Color.Black, 0), dottedLineBorder);
            ComplexBorder complexBorder = new ComplexBorder(new ComplexBorderSide(ComplexBorderSideStyle.Dotted, Color.Black),
                new ComplexBorderSide(Color.Black, 1), new ComplexBorderSide(ComplexBorderSideStyle.Dotted, Color.Black),
                new ComplexBorderSide(ComplexBorderSideStyle.Dotted, Color.Black));

            sheet.Cells[1, 1, 18, 10].Border = thinLineBorder;
            sheet.Cells[4, 4, 6, 9].Border = compoundBorder;
            sheet.Cells[7, 4, 9, 9].Border = compoundBorder;
            sheet.Cells[11, 4, 13, 9].Border = compoundBorder;
            sheet.Cells[14, 4, 16, 9].Border = compoundBorder;

            sheet.Cells[7, 4, 7, 9].Border = complexBorder;
            sheet.Cells[14, 4, 14, 9].Border = complexBorder;

            FillSampleData(sheet, new CellRange(4, 4, 6, 6));
            FillSampleData(sheet, new CellRange(11, 4, 6, 6));

            sheet.Columns[0, 1].Width = 40;
            sheet.Columns[3, 4].Width = 40;
            sheet.Columns[11].Width = 40;

            AddPieChart(sheet);

            fpSpread.ResumeLayout(true);
        }

        private void AddPieChart(SheetView sheet)
        {
            sheet.Cells[20, 1].Formula = "SUM(K5:K7)";
            sheet.Cells[20, 2].Formula = "SUM(K8:K10)";
            sheet.Cells[20, 3].Formula = "SUM(K12:K14)";
            sheet.Cells[20, 4].Formula = "SUM(K15:K17)";
            sheet.Rows[20].Visible = false;

            Point location = GetCellLocation(sheet, 2, 12);

            var chart = sheet.AddChart(new CellRange(20, 1, 1, 4), typeof(PieSeries), 300, 200, location.X, location.Y,
                ChartViewType.View2D, false);
            chart.DataSetting.ShowHiddenData = true;
        }

        private Point GetCellLocation(SheetView sheet, int row, int col)
        {
            float x = 0, y = 0;
            for (int j = 0; j < col; ++j)
                x += sheet.Columns[j].Width;

            for (int i = 0; i < row; ++i)
                y += sheet.Rows[i].Height;

            return new Point((int)x, (int)y);
        }

        private void FillSampleData(SheetView sheet, CellRange range)
        {
            Random random = new Random();
            for (int i = 0; i < range.RowCount; ++i)
            {
                for (int j = 0; j < range.ColumnCount; ++j)
                    sheet.Cells[i + range.Row, j + range.Column].Value = random.Next(50, 300);
            }
        }
        #endregion

        #region Event handlers
        private void BtnPrint_Click(object sender, EventArgs e)
        {
            var pi = fpSpread.ActiveSheet.PrintInfo;
            pi.ShowPrintDialog = true;
            pi.ShowColor = true;
            pi.EnhancePreview = pi.Preview = chkPrintPreview.Checked;

            fpSpread.PrintSheet(fpSpread.ActiveSheet);
        }
        #endregion
    }
}


//
//IntroPrint.Designer.cs
//


namespace Spread.Printing.Features
{
    partial class IntroPrint
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.fpSpread = new FarPoint.Win.Spread.FpSpread();
            this.fpSpread_Introduction = new FarPoint.Win.Spread.SheetView();
            this.btnPrint = new System.Windows.Forms.Button();
            this.chkPrintPreview = new System.Windows.Forms.CheckBox();
            this.pnlMain.SuspendLayout();
            this.pnlControls.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Introduction)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.fpSpread);
            // 
            // pnlControls
            // 
            this.pnlControls.Controls.Add(this.btnPrint);
            this.pnlControls.Controls.Add(this.chkPrintPreview);
            // 
            // fpSpread
            // 
            this.fpSpread.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread.Location = new System.Drawing.Point(0, 0);
            this.fpSpread.Name = "fpSpread";
            this.fpSpread.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.fpSpread_Introduction});
            this.fpSpread.Size = new System.Drawing.Size(477, 487);
            this.fpSpread.TabIndex = 0;
            // 
            // fpSpread_Introduction
            // 
            this.fpSpread_Introduction.Reset();
            this.fpSpread_Introduction.SheetName = "Introduction";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.fpSpread_Introduction.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.fpSpread_Introduction.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // btnPrint
            // 
            this.btnPrint.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.btnPrint.Location = new System.Drawing.Point(16, 22);
            this.btnPrint.Name = "btnPrint";
            this.btnPrint.Size = new System.Drawing.Size(128, 23);
            this.btnPrint.TabIndex = 0;
            this.btnPrint.Text = "Print Sheet";
            this.btnPrint.UseVisualStyleBackColor = true;
            // 
            // chkPrintPreview
            // 
            this.chkPrintPreview.AutoSize = true;
            this.chkPrintPreview.Checked = true;
            this.chkPrintPreview.Location = new System.Drawing.Point(6, 52);
            this.chkPrintPreview.Name = "chkPrintPreview";
            this.chkPrintPreview.Size = new System.Drawing.Size(53, 17);
            this.chkPrintPreview.TabIndex = 1;
            this.chkPrintPreview.Text = "Print Preview";
            this.chkPrintPreview.UseVisualStyleBackColor = true;
            // 
            // IntroPrint
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "IntroPrint";
            this.pnlMain.ResumeLayout(false);
            this.pnlControls.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Introduction)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread fpSpread;
        private FarPoint.Win.Spread.SheetView fpSpread_Introduction;
        private System.Windows.Forms.Button btnPrint;
        private System.Windows.Forms.CheckBox chkPrintPreview;
    }
}
